package services;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.session.SqlSession;

import play.libs.Codec;

import com.aton.db.SessionFactory;

import models.DrCase;
import models.DrCaseContent;
import models.DrCourt;
import models.DrLiveTemp;
import models.DrLiveTempExample;
import models.SystemLog;
import models.SystemOperator;
import models.mapper.DrCaseContentMapper;
import models.mapper.DrCaseMapper;
import models.mapper.DrCourtMapper;
import models.mapper.DrLiveTempMapper;
import models.mapper.SystemLogMapper;

/**
 * 庭审直播控制类
 * @author wangshaoming
 *
 */
public class CourtLiveService {
	/**
	 * 获取临时数据
	 * @param lastTime
	 * @param caseid 
	 * @return
	 */
	public static List<DrLiveTemp> getTemp(Long lastTime, String caseid) {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			DrLiveTempMapper mapper=ss.getMapper(DrLiveTempMapper.class);
			DrLiveTempExample example=new DrLiveTempExample();
			DrLiveTempExample.Criteria c=example.createCriteria();
			if(lastTime!=null){
				c.andBeginTimeGreaterThanOrEqualTo(BigDecimal.valueOf(lastTime));
			}
			example.setOrderByClause("BEGIN_TIME asc");
			c.andCaseidEqualTo(caseid);
			return mapper.selectByExample(example);
		}finally{
			ss.close();
		}
	}
	/**
	 * 获取最后一次读写记录
	 * @param caseid 
	 * @return
	 */
	public static DrLiveTemp getLastOne(String caseid) {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			DrLiveTempMapper mapper=ss.getMapper(DrLiveTempMapper.class);
			return mapper.getLastOne(caseid);
		}finally{
			ss.close();
		}
	}
	/**
	 * 保存读写信息
	 * @param listCase
	 * @param caseid 案件id
	 */
	public static void saveTemp(List<DrLiveTemp> listCase,String caseid) {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			DrLiveTempMapper mapper=ss.getMapper(DrLiveTempMapper.class);
			for(DrLiveTemp temp : listCase){
				temp.setCreatetime(new Date());
				temp.setCaseid(caseid);
				//对已经存在的进行更新
				if(temp.getIsHas()!=null&&temp.getIsHas()){
					mapper.updateByPrimaryKeySelective(temp);
				}else{
					temp.setTempid(Codec.UUID());
					mapper.insert(temp);
				}
			}
			ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			ss.rollback();
		}finally{
			ss.close();
		}
	}
	/**
	 * 闭庭操作
	 * @param newCase 更新的案件
	 * @param cases 原始案件信息
	 * @param court 法庭信息
	 * @param content 内容
	 * @param loginUser 操作人信息
	 * @param ip 操作人ip
	 * @return
	 */
	public static boolean stopLive(DrCase newCase, DrCase cases,
			DrCourt court, String content, SystemOperator loginUser, String ip) {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			DrCaseMapper caseMapper=ss.getMapper(DrCaseMapper.class);
			DrCourtMapper courtmapper=ss.getMapper(DrCourtMapper.class);
			caseMapper.updateByPrimaryKeySelective(newCase);
			DrCourt newCourt=new DrCourt();
			newCourt.setCourtid(court.getCourtid());
			//更新法庭为未使用
			newCourt.setStatus(new BigDecimal(1));
			courtmapper.updateByPrimaryKeySelective(newCourt);
			//保存转写内容
			DrCaseContentMapper contentMapper=ss.getMapper(DrCaseContentMapper.class);
			DrCaseContent cont=new DrCaseContent();
			cont.setCaseid(cases.getCaseid());
			cont.setContent(content);
			if(DrCaseContentService.getByCaseid(cases.getCaseid()) == null){
				contentMapper.insert(cont);
			}
			else{
				contentMapper.updateByPrimaryKeySelective(cont);
			}
			//删除临时存放数据
			DrLiveTempMapper mapper=ss.getMapper(DrLiveTempMapper.class);
			DrLiveTempExample example=new DrLiveTempExample();
			DrLiveTempExample.Criteria c=example.createCriteria();
			c.andCaseidEqualTo(cases.getCaseid());
			mapper.deleteByExample(example);
			//插入日志
			SystemLog log=new SystemLog(Codec.UUID(),loginUser.getOperatorid(),ip,"闭庭","闭庭释放法庭："+court.getCourtname()+"闭庭案件为："+cases.getCaseid(),new Date());
			SystemLogMapper logMapper=ss.getMapper(SystemLogMapper.class);
			logMapper.insert(log);
			ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			ss.rollback();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	
	/**
	 * 更新临时存放表
	 * @param temp
	 */
	public static void updateTemp(DrLiveTemp temp){
		SqlSession ss=SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			DrLiveTempMapper mapper=ss.getMapper(DrLiveTempMapper.class);
			mapper.updateByPrimaryKeySelective(temp);
			ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
		}finally{
			ss.close();
		}
	}
	/**
	 * 插入临时存放表
	 * @param temp
	 */
	public static void insertTemp(DrLiveTemp temp) {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			DrLiveTempMapper mapper=ss.getMapper(DrLiveTempMapper.class);
			mapper.insert(temp);
			ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
		}finally{
			ss.close();
		}
	}
}
